package com.unicom.office.unit;

import java.util.ArrayList;
import java.util.List;

public abstract class OfficeUnit {
    protected Object source;
    protected boolean justResource;
    protected OfficeUnit parent;
    protected OfficeUnit prev;
    protected OfficeUnit next;
    protected List<OfficeUnit> children;
    protected boolean matched;

    public OfficeUnit(){
        children = new ArrayList<OfficeUnit>();
    }

    public void addChildren(List<OfficeUnit> children){
        for (OfficeUnit child : children) {
            addChild(child);
        }
    }

    public void addChild(OfficeUnit child){
        if(!testChildType(child)) {
            throw new RuntimeException("添加的子节点类型不匹配");
        }
        child.parent = this;
        if(children.size() > 0) {
            OfficeUnit last = children.get(children.size() - 1);
            last.next = child;
            child.prev = last;
        }
        children.add(child);
    }

    public Object getSource() {
        return source;
    }

    public void setSource(Object source) {
        this.source = source;
    }

    public boolean isJustResource() {
        return justResource;
    }

    public void setJustResource(boolean justResource) {
        this.justResource = justResource;
    }

    public OfficeUnit getParent() {
        return parent;
    }

    public void setParent(OfficeUnit parent) {
        this.parent = parent;
    }

    public OfficeUnit getPrev() {
        return prev;
    }

    public OfficeUnit getNext() {
        return next;
    }

    public List<OfficeUnit> getChildren() {
        return children;
    }

    public boolean isMatched() {
        return matched;
    }

    public void setMatched(boolean matched) {
        this.matched = matched;
    }

    protected abstract boolean testChildType(OfficeUnit child);
}
